package com.chuantou.yuba.util;

import java.lang.reflect.Field;
import java.util.List;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.chuantou.yuba.R;
import com.chuantou.yuba.ui.LoginActivity;

public class UiUtil {

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public static int getActionBarHeight(Context context) {
		TypedValue mTypedValue = new TypedValue();
		int actionBarHeight = 0;
		if (Utils.hasHoneycomb()) {
			context.getTheme().resolveAttribute(android.R.attr.actionBarSize, mTypedValue, true);
			actionBarHeight = TypedValue.complexToDimensionPixelSize(mTypedValue.data, context.getResources().getDisplayMetrics());
		} else {
			actionBarHeight = context.getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height);
		}

		return actionBarHeight;
	}

	// 获取手机状态栏高度
	public static int getStatusBarHeight(Context context) {
		Class<?> c = null;
		Object obj = null;
		Field field = null;
		int x = 0, statusBarHeight = 0;
		try {
			c = Class.forName("com.android.internal.R$dimen");
			obj = c.newInstance();
			field = c.getField("status_bar_height");
			x = Integer.parseInt(field.get(obj).toString());
			statusBarHeight = context.getResources().getDimensionPixelSize(x);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return statusBarHeight;
	}

	/**
	 * 弹出登录提示框
	 * 
	 * @param activity
	 * @param requestCode
	 * @param action
	 */
	public static void showNoLoginDialog(final Activity activity, final int requestCode, final int action) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle(R.string.prompt);
		builder.setMessage(R.string.prompt_message_no_login);
		builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.setPositiveButton(R.string.login_now, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				LoginActivity.start(activity, requestCode, action);
			}
		});
		builder.create().show();
	}

	/**
	 * 弹出登录提示框
	 * 
	 * @param activity
	 * @param requestCode
	 * @param action
	 */
	public static void showNoLoginDialog(final Fragment fragment, final int requestCode, final int action) {
		AlertDialog.Builder builder = new AlertDialog.Builder(fragment.getActivity());
		builder.setTitle(R.string.prompt);
		builder.setMessage(R.string.prompt_message_no_login);
		builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.setPositiveButton(R.string.login_now, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				LoginActivity.start(fragment, requestCode, action);
			}
		});
		builder.create().show();
	}

	private static long sLastClickTime;

	public static boolean isFastDoubleClick() {
		long time = System.currentTimeMillis();
		long timeD = time - sLastClickTime;
		if (0 < timeD && timeD < 500) {
			return true;
		}
		sLastClickTime = time;
		return false;
	}

	public static void copy(Context context, String text, boolean showToast) {
		ClipboardManager c = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		c.setPrimaryClip(ClipData.newPlainText(null, text));
		if (showToast) {
			Toast.makeText(context, R.string.text_has_copy, Toast.LENGTH_SHORT).show();
		}
	}

	public static void showInputKeyboard(Context context, View view) {
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
		imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
	}

	public static void hideInputKeyboard(Context context, View view) {
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}

	public static int dip2px(Context context, float f) {
		return (int) (0.5D + (double) (f * getDensity(context)));
	}

	public static int dip2px(Context context, int i) {
		return (int) (0.5D + (double) (getDensity(context) * (float) i));
	}

	public static int get480Height(Context context, int i) {
		return (i * getScreenWidth(context)) / 480;
	}

	public static float getDensity(Context context) {
		return context.getResources().getDisplayMetrics().density;
	}

	public static int getScal(Context context) {
		return (100 * getScreenWidth(context)) / 480;
	}

	public static int getScreenDensityDpi(Context context) {
		return context.getResources().getDisplayMetrics().densityDpi;
	}

	public static int getScreenHeight(Context context) {
		return context.getResources().getDisplayMetrics().heightPixels;
	}

	public static int getScreenWidth(Context context) {
		return context.getResources().getDisplayMetrics().widthPixels;
	}

	public static float getXdpi(Context context) {
		return context.getResources().getDisplayMetrics().xdpi;
	}

	public static float getYdpi(Context context) {
		return context.getResources().getDisplayMetrics().ydpi;
	}

	public static int px2dip(Context context, float f) {
		float f1 = getDensity(context);
		return (int) (((double) f - 0.5D) / (double) f1);
	}

	public static int px2dip(Context context, int i) {
		float f = getDensity(context);
		return (int) (((double) i - 0.5D) / (double) f);
	}
	
	public static boolean isIntentAvailable(Context context, Intent intent) {
		final PackageManager packageManager = context.getPackageManager();
		List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
		if (resolveInfo.size() > 0) {
			return true;
		}
		return false;
	}
}
